﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using Bon.It.Proxy;
using Bon_It.Model;

namespace Bonit_It.ViewModel.MasterData
{
    public class ComodityGroupsViewModel : Base.ViewModel
    {
        public ObservableCollection<Warengruppe> ComodityGroups { get; set; }

        public Warengruppe SelectedItem { get; set; }

        public ComodityGroupsViewModel()
        {
            this.RefreshLookups();

            DisplayName = string.Format("Warengruppen");
        }
        
        #region Overrides of ViewModel


       

        public override void RefreshLookups()
        {

            ComodityGroups =new ObservableCollection<Warengruppe>(ServiceProxy.Instance.GetComodityGroups());
            if (ComodityGroups.Count > 0)
            {
                SelectedItem = ComodityGroups[0];
            }
        }

        public override void CleanUp()
        {
            ComodityGroups = null;
        }

        public override bool CanDelete(object argument)
        {

            return SelectedItem != null && SelectedItem.Artikel.Count.Equals(0) && SelectedItem.Id.Equals(0) ==false;
        }

        public override bool CanSave(object argument)
        {
               if (SelectedItem != null)
               {
                   return string.IsNullOrEmpty(SelectedItem.Error);
               }
            return false;
        }

        public override bool CanCreate(object argument)
        {
            return true;
        }

        #endregion

        #region overrides
        public override void OnDelete(object argument)
        {
            Debug.Assert(SelectedItem != null, "The item is null...");

            if (BeforeDelete() == MessageBoxResult.Yes)
            {
                ServiceProxy.Instance.DeleteComodityGroup(SelectedItem);
            }
            ComodityGroups.Remove(SelectedItem);

        }

        public override void OnSave(object argument)
        {
            Debug.Assert(SelectedItem != null, "The item is null...");
            SelectedItem.CanBeModified = true;
            ServiceProxy.Instance.StoreComoditiyGroup(SelectedItem);
            base.OnSave(argument);
        }
        public override void OnCreate(object argument)
        {
            Warengruppe wgr = new Warengruppe();     
            ComodityGroups.Add(wgr);
            SelectedItem = wgr;
        }

        #endregion
    }
}
